import {
  requestUtil
} from "../../utils/requestUtil"

import {
  getAddressByUserIdAPI,
  setDefaultAddressAPI
} from '../../api/address'

import {
  addCartAPI
} from '../../api/cart'

import {
  addStarAPI,
  deleteStarAPI,
  checkIsStarAPI
} from '../../api/star'

import {
  getRawByProIdAPI,
  getSupplierByRawIdAPI
} from '../../api/raw'

import {
  addOrderAPI
} from '../../api/order'

import {
  getCommentByProIdAPI,
  addCommentAPI
} from '../../api/comment'

Page({
  data: {
    active: 1,
  },

});

Component({
  options: {
    styleIsolation: 'apply-shared',
  },

  data: {
    third: 1,
    id: 0,
    userId: 0,
    productInfo: null,
    records: null,
    showPopCar: false,
    showPopBuy: false,
    showPopAddress: false,
    addressList: [],
    commentList: [],
    rawList: [],
    defaultAddress: null,
    content: '',
    radio: '1',
    num: 1,
    isStar: false,
    starId: 0,
    supplierInfo: null

  },
  methods: {
    onThirdChange(e) {
      this.setData({
        third: e.detail.current
      });
    },
    async getProductById(id) {
      await requestUtil({
        url: `/product/${id}`,
        method: 'GET',
      }).then(res => {
        console.log(res);
        this.setData({
          productInfo: res.result,
        });
      })
    },
    getRecordsByProId(id) {
      requestUtil({
        url: `/record/${id}`,
        method: 'GET',
      }).then(res => {
        this.getSupplierByRawId(res.result[0].id)
        this.setData({
          records: res.result,
        });
      })
    },
    async getSupplierByRawId(rawId) {
      await getSupplierByRawIdAPI(rawId).then(res => {
        if (res.code == 200) {
          this.setData({
            supplierInfo: res.result,
          })
        }
      })
    },
    async getAddressByUserId(userId) {
      await getAddressByUserIdAPI(userId).then(res => {
        if (res.code == 200) {
          this.setData({
            addressList: res.result,
            defaultAddress: res.result[0],
            radio: res.result[0].id
          })
        }
      });

    },
    async checkIsStar(userId, proId) {
      await checkIsStarAPI(userId, proId).then(res => {
        if (res.code == 200) {
          this.setData({
            isStar: true,
            starId: res.result.id
          })
        } else {
          this.setData({
            isStar: false,
          })
        }
      });
    },
    async getCommentByProId(proId) {
      await getCommentByProIdAPI(proId).then(res => {
        if (res.code == 200) {
          this.setData({
            commentList: res.result,
          })
        }
      });
    },
    async getRawByProId(proId) {
      let that = this
      await getRawByProIdAPI(proId).then(res => {
        if (res.code == 200) {
          this.setData({
            rawList: res.result,
          })
          if (that.data.rawList.length != 0) {
            that.getRecordsByProId(that.data.rawList[0].rawId)
          }
        }
      });
    },
    onSetDefault(event) {
      let address = {
        id: this.data.radio,
        userId: this.data.userId
      }
      let that = this
      setDefaultAddressAPI(address).then(res => {
        if (res.code == 200) {
          that.getAddressByUserId(that.data.userId)

          that.setData({
            showPopAddress: false
          })
        } else {
          wx.showToast({
            title: '设置失败',
          })
        }
      })
      console.log("value：" + this.data.radio)
    },
    onChangeStepper(event) {
      this.setData({
        num: event.detail,
      });
    },
    onChangeRadio(event) {
      console.log(event.detail);
      this.setData({
        radio: event.detail,
      });
    },
    onClickRadio(event) {
      const {
        name
      } = event.currentTarget.dataset;
      this.setData({
        radio: name,
      });
    },
    onClickStar() {
      if (this.data.isStar) {
        let StarObj = {
          userId: this.data.userId,
          proId: this.data.id
        }
        deleteStarAPI(StarObj).then(res => {
          if (res.code == 200) {
            wx.showToast({
              title: '取消收藏',
            })
          }
        })
      } else {
        let StarObj = {
          userId: this.data.userId,
          proId: this.data.id
        }
        addStarAPI(StarObj).then(res => {
          if (res.code == 200) {
            wx.showToast({
              title: '收藏成功',
            })
          }
        })
      }
      this.setData({
        isStar: !this.data.isStar
      });

    },
    onClickCar() {
      wx.switchTab({
        url: '/pages/car/car',
      })
    },
    onClosePopCar() {
      this.setData({
        showPopCar: false
      });
    },
    onClosePopBuy() {
      this.setData({
        showPopBuy: false
      });
    },
    onClosePopAddress() {
      this.setData({
        showPopAddress: false
      });
    },
    onShowAddressPop() {
      this.setData({
        showPopAddress: true
      });
    },
    onAddCar() {
      this.setData({
        showPopCar: true
      });
    },
    onBuy() {
      this.setData({
        showPopBuy: true
      });
    },
    submitCar() {
      let cartObj = {
        num: this.data.num,
        userId: this.data.userId,
        proId: this.data.id,
        addId: this.data.radio
      }
      addCartAPI(cartObj).then(res => {
        if (res.code == 200) {
          wx.showToast({
            title: '已加入购物车',
          })
        }
      })
      this.setData({
        showPopCar: false
      });
    },
    submitBuy() {
      let orderObj = {
        num: this.data.num,
        userId: this.data.userId,
        proId: this.data.id,
        addId: this.data.radio,
        totalPrice: this.data.num * this.data.productInfo.price
      }
      addOrderAPI(orderObj).then(res => {
        if (res.code == 200) {
          wx.showToast({
            title: '购买成功',
          })
        }
      })
      this.setData({
        showPopBuy: false
      });
    },
    async onSendComment() {
      let comment = {
        userId: this.data.userId,
        proId: this.data.id,
        content: this.data.content
      }
      let that = this
      await addCommentAPI(comment).then(res => {
        if (res.code == 200) {
          wx.showToast({
            title: '发送成功',
          })
          that.getCommentByProId(that.data.id)
        }
      })
      console.log(this.data.content);
    },
    onCommentInput(e) {
      this.setData({
        content: e.detail.value
      })
    },
    onTabsClick(event) {
      this.getSupplierByRawId(event.detail.value)
      this.getRecordsByProId(event.detail.value)
      console.log(event.detail.value);
    },
    onLoad: function (options) {
      let user = wx.getStorageSync('user')
      if (user) {
        this.setData({
          userId: user.id
        })
      }
      this.setData({
        id: options.id,
      });
      this.getProductById(options.id)

      this.getProductById(options.id)

      this.getAddressByUserId(this.data.userId)
      this.getCommentByProId(options.id)
      this.getRawByProId(options.id)
      this.checkIsStar(this.data.userId, options.id)

      // this.getSupplierByRawId(this.data.rawList[0].id)

    },
    onShow() {
      this.onLoad()
      console.log("onShow");
    }
  },


});